package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.FileParser;
import model.Manuscript;
import model.Review;
import model.State;
import model.Status;

@SuppressWarnings("serial")
/*
 * Creates a new review panel.
 * @author Jeff Kuhlman
 */
public class NewReviewPanel extends JPanel {
	
	private State my_state;
	private JFrame my_frame;
	private Manuscript my_manuscript;
	private JTextField my_file_path;
	
	public NewReviewPanel(State the_state, JFrame the_frame, Manuscript the_manuscript) {
		super();
		my_state = the_state;
		my_frame = the_frame;
		my_manuscript = the_manuscript;
		setup();
	}

	public void setup() {
		GridLayout exp_layout = new GridLayout(0,2);
		my_file_path = new JTextField();
		JFileChooser fc = new JFileChooser();
		BrowseAction browse = new BrowseAction(my_frame, fc, my_file_path);
		JButton bb = new JButton(browse);
		
		this.setLayout(exp_layout);
		this.add(new JLabel("Reviewer's Name"));
		this.add(new JLabel(my_state.getUser().getName()));
		this.add(new JLabel("Paper Name"));
		this.add(new JLabel(my_manuscript.getMy_title()));
		this.add(new JLabel("Conference"));
		this.add(new JLabel(my_state.getConference().getConfName()));
		this.add(my_file_path);
		this.add(bb);
		this.add(new JButton(new CancelAction()));
		this.add(new JButton(new SubmitAction()));
	}
	
	private boolean verify()
	{
		if (my_file_path.getText().trim().length() == 0)
			JOptionPane.showMessageDialog(this, "Please select a file");
		
		return (my_file_path.getText().trim().length() > 0);
	}
	
	private class SubmitAction extends AbstractAction
	{
		
		public SubmitAction()
		{
			super("Submit");
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (verify())
			{

				Review review = new Review(FileParser.NEW_ELEMENT, my_manuscript.getMy_id(), 
										   my_state.getUser().getID(), my_file_path.getText());
				if(my_state.getParser().writeReview(review))
				{
					my_manuscript.setMy_status(Status.REVIEWED);
					my_state.getParser().writeManuscript(my_manuscript);
					JOptionPane.showMessageDialog(NewReviewPanel.this, "You Have successfully Uploaded your review");
					my_frame.dispose();
				} else {
					JOptionPane.showMessageDialog(NewReviewPanel.this, "Couldn't upload file.  Try again.");	
				}
			}
		}
		
	}
	
	private class CancelAction extends AbstractAction
	{

		public CancelAction()
		{
			super("Cancel");
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			my_frame.dispose();
			
		}
		
	}
	

}
